package com.example.room.repository;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import com.example.room.dao.PublicTeamDao;
import com.example.room.database.MyDatabase;
import com.example.room.entity.PublicTeam;

import java.util.List;

public class PublicTeamRepository {
    LiveData<List<PublicTeam>> teams;

    PublicTeamDao publicTeamDao;

    public PublicTeamRepository(Context context) {
        publicTeamDao = MyDatabase.getInstance(context).getPublicTeamDao();
        teams = publicTeamDao.getAllTeam();
    }

    public LiveData<List<PublicTeam>> getAllTeams() {
        return teams;
    }

    public void deleteAllTeam() {
        new DeleteAllTeamAsyncTask(publicTeamDao).execute();
    }

    public static class DeleteAllTeamAsyncTask extends AsyncTask<Void,Void,Void> {
        PublicTeamDao publicTeamDao;

        public DeleteAllTeamAsyncTask(PublicTeamDao publicTeamDao) {
            this.publicTeamDao = publicTeamDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            publicTeamDao.deleteAllTeam();
            return null;
        }
    }

    public void insertTeam(PublicTeam... publicTeam) {
        new InsertTeamAsyncTask(publicTeamDao).execute(publicTeam);
    }

    public static class InsertTeamAsyncTask extends AsyncTask<PublicTeam,Void,Void> {
        PublicTeamDao publicTeamDao;

        public InsertTeamAsyncTask(PublicTeamDao publicTeamDao) {
            this.publicTeamDao = publicTeamDao;
        }

        @Override
        protected Void doInBackground(PublicTeam... publicTeams) {
            publicTeamDao.insertTeam(publicTeams);
            return null;
        }
    }
}
